from flask import Flask,request
from xiaoyiwy.xiaoyi.search import *
from xiaoyiwy.xiaoyi.login import *
from xiaoyiwy.xiaoyi.drugbox import *
from xiaoyiwy.xiaoyi.article import *
from xiaoyiwy.xiaoyi.balance import *

import re

app = Flask(__name__)


# 获取openid,sessionkey
@app.route('/login/<data>', methods=['POST'])
def login(data):
    return gain_openid(request.get_json(data))

# 存储用户信息
@app.route('/login/wxdata/<data>', methods=['POST'])
def save_user_info(data):
    return wxdata(request.get_json(data))

# 列表/详情搜索
@app.route('/search/drugname/<text>',methods=['POST'])
def search_text(text):
    return search(request.get_json(text))

# 历史搜索
@app.route('/search/history/<text>',methods=['POST'])
def history_search(text):
    return history(request.get_json(text))

# 历史扫描
@app.route('/history/qrcode/<id>',methods=['POST'])
def qrcode_history(id):
    return history_qrcode(request.get_json(id))

# 热搜
@app.route('/search/hot',methods=['GET'])
def hot_search():
    return hot()

# 对比
@app.route('/contrast/<id>',methods=['POST'])
def contrast_search(id):
    return contrast(request.get_json(id))

# 扫码
@app.route('/search/qrcode/<img>',methods=['POST'])
def search_img(img):
    return scan(request.get_json(img))

# 文章搜索
@app.route('/search/article/<text>',methods=['GET','POST'])
def article(text):
    if request.method == 'GET':
        return article_get()
    else:
        return article_post(request.get_json(text))

# 文章热搜
@app.route('/article/hot',methods=['GET'])
def article_hot():
    return article_hot_get()

# 医药箱查看/加入/移除
@app.route('/drugbox/<text>',methods=['POST'])
def drugbox(text):
    type = request.get_json()['type']
    openid = request.get_json()['openid']
    if type == '0':
        if openid:
            openid = int(openid)
            return drug_box_list(openid)
        else:
            return { "error": "openid为空" }
    else:
        data = request.get_json()['data']
        open_id = int(openid)
        drug_id = data['drug_id']
        drug_name = data['drug_name']
        delete = data['delete']
        if open_id:
            if drug_id:
                if drug_name:
                    drug_id = int(drug_id)
                    return drug_box(open_id,drug_id,drug_name,delete)
                else:
                    return { "error":"药品名称为空" }
            else:
                return { "error": "药品id为空"}
        else:
            return {"error":"openid为空"}

# 积分
@app.route('/balance/<id>',methods=['POST'])
def balance(id):
    return balance_post(request.get_json(id))


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, debug=True)
